Skip to content

Conversation

@febo
Copy link
Collaborator

@febo febo commented Nov 18, 2025

Problem

Creating accounts funded by a payer account is a common operation. In most cases, the logic needs to be replicated in different programs.

Solution

Add a helper to create accounts. The helper supports creating both PDA and non-PDA accounts.

@febo febo force-pushed the febo/create-account-helper branch from 4c01fc7 to 4b87477 Compare November 18, 2025 13:52
@febo febo marked this pull request as draft November 18, 2025 13:55
@febo febo closed this Nov 18, 2025
@febo febo force-pushed the febo/create-account-helper branch from 4b87477 to 05f74a9 Compare November 18, 2025 13:58
@febo febo reopened this Nov 18, 2025
@febo febo marked this pull request as ready for review November 18, 2025 14:04
to: account,
lamports: required_lamports,
}
.invoke()?;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we make this .invoke_signed(signers)?; to support the case a PDA acts as the funding account for the transfer?

Rent::get()?.minimum_balance(space)
};

if account.lamports() == 0 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what do you think about using hints from pinocchio::hint module?

i expect account is not exist, in the majority of cases

Suggested change
if account.lamports() == 0 {
if likely(account.lamports() == 0) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what do you think about using hints from pinocchio::hint module?

i expect account is not exist, in the majority of cases

It made no difference in my tests. Also, I had a look at the asm and it seems the compiler is doing the right thing already. Did you notice a difference?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the same for the lib (i didn't test it in the real app).
As a part of library it compiles in the expected instructions.
Just wanted to make extra sure this logic won't be miscompiled in the large program.

let required_lamports = lamports.saturating_sub(account.lamports());

// Transfer lamports from `payer` to `account` if needed.
if required_lamports > 0 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the same here can be applied, i believe.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thing here, no difference.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants